//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class Window2Record : WorksheetLevelRecord
    {
        public Window2Record()
        {

        }

        public Window2Record(Worksheet worksheet)
        {
            if (worksheet.Workbook.View == null)
                worksheet.Workbook.View = new WorkbookView(worksheet.Workbook);
            if (worksheet.View == null)
                worksheet.View = new WorksheetView(worksheet);
            //\\todo: formula
            fDspGridRt = worksheet.View.GridLinesVisible ;
            fDspRwColRt = worksheet.View.RowColumnHeadersVisible ;
            if (worksheet.View.Pane != null)
            {
                switch (worksheet.View.Pane.State)
                {
                    case PaneState.Frozen:
                        this.fFrozenRt = true;
                        this.fFrozenNoSplit = true;
                        break;
                    case PaneState.FrozenSplit:
                        this.fFrozenRt = true;
                        this.FFrozenNoSplit = false;
                        break;
                    case PaneState.Split:
                        this.fFrozenRt = false;
                        this.fFrozenNoSplit = false;
                        break;
                }
            }
            //\\todo: pane
            fDspZerosRt = worksheet.View.ZerosVisible;
            //\\
            //\\
            this.rwTop = (ushort) worksheet.ActivePane.Top ;
             colLeft = (ushort) worksheet.ActivePane.Left;
            //\\zoom
            wScaleNormal = (ushort) worksheet.View.ZoomScaleNormal ;
            this.fSelected = worksheet.Workbook.View.ActiveSheet == worksheet;

        }
        private ushort colLeft;
        private bool fDspFmlaRt;
        private bool fDspGridRt;
        private bool fDspRwColRt;
        private bool fFrozenRt;
        private bool fDspZerosRt;
        private bool fDefaultHdr = true;
        private bool fRightToLeft;
        private bool fDspGuts = true;
        private bool fFrozenNoSplit;
        private bool fSelected;
        private bool fPaged = true;
        private bool fSLV
            
            ;

        private ushort rwTop;
        private ushort icvHdr = 64;
        private ushort wScaleSLV;
        private ushort wScaleNormal;
        private ushort reserved3;
        private ushort unused;


        internal override void Apply(BiffReaderWorksheetContext context)
        {
            var worksheet = context.Worksheet;
            if (worksheet.Workbook.View == null)
                worksheet.Workbook.View = new WorkbookView(worksheet.Workbook);
            if (worksheet.View == null)
                worksheet.View = new WorksheetView(worksheet);
            //\\todo: formula
            worksheet.View.GridLinesVisible = fDspGridRt;
            worksheet.View.RowColumnHeadersVisible = fDspRwColRt;
            //\\todo: pane
            worksheet.View.ZerosVisible = fDspZerosRt;
            //\\
            //\\
            worksheet.ActivePane.Top = this.rwTop;
            worksheet.ActivePane.Left = colLeft;
            //\\zoom
            worksheet.View.ZoomScaleNormal = wScaleNormal;
            context.LastWindow2Record = this;

        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Window2; }
        }

        public bool FFrozenRt
        {
            get { return fFrozenRt; }
        }

        public bool FFrozenNoSplit
        {
            get { return fFrozenNoSplit; }
            set { fFrozenNoSplit = value; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            ushort am = 0;
            if (fDspFmlaRt)
                am |= 0x0001;
            if (fDspGridRt) am |= 0x0002;
            if (fDspRwColRt) am |= 0x0004;
            if (FFrozenRt ) am |= 0x0008;
            if (fDspZerosRt) am |= 0x0010;
            if (fDefaultHdr) am |= 0x0020;
            if (fRightToLeft) am |= 0x0040;
            if (fDspGuts) am |= 0x0080;
            if (FFrozenNoSplit) am |= 0x0100;
            if (fSelected) am |= 0x0200;
            if (fPaged) am |= 0x0400;
            if (fSLV) am |= 0x0800;
            context.Write(am);
            context.Write(rwTop);
            context.Write(colLeft);
            context.Write(icvHdr);
            ushort reserved2 = 0;
            context.Write(reserved2);

            context.Write(this.wScaleSLV);
            context.Write(this.wScaleNormal);
            context.Write(unused);
            context.Write(reserved3);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            ushort am = context.ReadUInt16();
            fDspFmlaRt = (am & 0x0001) != 0;
            fDspGridRt = (am & 0x0002) != 0;
            fDspRwColRt = (am & 0x0004) != 0;
            fFrozenRt = (am & 0x0008) != 0;
            fDspZerosRt = (am & 0x0010) != 0;
            fDefaultHdr = (am & 0x0020) != 0;
            fRightToLeft = (am & 0x0040) != 0;
            fDspGuts = (am & 0x0080) != 0;
            FFrozenNoSplit = (am & 0x0100) != 0;
            fSelected = (am & 0x0200) != 0;
            fPaged = (am & 0x0400) != 0;
            fSLV = (am & 0x0800) != 0;

            rwTop = context.ReadUInt16();
            colLeft = context.ReadUInt16();
            icvHdr = context.ReadUInt16();
            var reserved2 = context.ReadUInt16();
            Contract.Assert(reserved2 == 0);

            this.wScaleSLV = context.ReadUInt16();
            this.wScaleNormal = context.ReadUInt16();
            this.unused = context.ReadUInt16();//\\none if chart substream;
            this.reserved3 = context.ReadUInt16();
            
        }
    }
}